package com.atguigu.mymvc.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class ClassUtil {
    public static  Object createInstance(String entityClassName){
        try {
            return  Class.forName(entityClassName).getDeclaredConstructor().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
//        return null;
    }
    public static  void  setProperty(Object instance,String propertyName,Object propertyValue){
        Class<?> aClass = instance.getClass();
        try {
            Field field = aClass.getDeclaredField(propertyName);
            field.setAccessible(true);
            field.set(instance,propertyValue);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
